package com.fhz.kotlinstest.truepaging

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.fhz.kotlinstest.Banner
import com.fhz.kotlinstest.Data
import com.fhz.kotlinstest.R
import javax.inject.Inject

private const val TAG = "AppStorePagingAdapter"
class AppStorePagingAdapter(): PagingDataAdapter<Data, AppStorePagingAdapter.VH>(
    DataDifferntiator
) {
    class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val appName: TextView = itemView.findViewById<TextView>(R.id.tv_app_name)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        Log.i(TAG, "onBindViewHolder: ")
        val bean = getItem(position)
        holder.appName.text = bean?.title
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_appstore_list, parent, false)
        return VH(view)
    }

    object DataDifferntiator : DiffUtil.ItemCallback<Data>() {

        override fun areItemsTheSame(
            oldItem: Data,
            newItem: Data
        ): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(
            oldItem: Data,
            newItem: Data
        ): Boolean {
            return oldItem.title == newItem.title
        }
    }

}